<?php
require_lib('sms.function');
require_model('sms/xmlclient.class');
class sms extends xmlclient{
	private $ctx;
	var $ConfNull="1";
	function __construct(){
		$this->ctx = $GLOBALS['context'];
		$vcp_server = $this->ctx->shop_conf->get('sms_app_address');
		$vcp_server_port = $this->ctx->shop_conf->get('sms_app_port');
		$vcp_user = $this->ctx->shop_conf->get('sms_app_user') ;
		$vcp_password = $this->ctx->shop_conf->get('sms_app_password');
		$this->ctx->log_error('vcp_server:'.$vcp_server.';vcp_server_port:'.$vcp_server_port.'; vcp_user:'.$vcp_user.';vcp_password:'.$vcp_password);
		if($vcp_server && $vcp_server_port && $vcp_user  && $vcp_password ) {
			$this->serverURL = $vcp_server.":".$vcp_server_port;
			$this->XMLType = "SMS";
			$this->VCP = $vcp_user;
			$this->VCPPassword = $vcp_password;
		} else {
			$this->ConfNull = "-1";
		}
	}
	//发送SMS短信接口
	function sendSMS($mobile, $msg, $time="", $apitype=0){
		$xml_command="<action>SMS:sendSMS</action>
						<sms:mobile>$mobile</sms:mobile>
						<sms:message>".base64_encode($msg)."</sms:message>
						<sms:datetime>$time</sms:datetime>
						<sms:apitype>$apitype</sms:apitype>";
		$this->sendSCPData($this->serverURL, $xml_command);
		$this->toPlain();
		//$result = $this->responseXML;
		//$GLOBALS['context']->log_error('返回信息:'.print_r($result));
		return 1;
	}
	//查询远程账户余额
	function infoSMSAccount(){
		$xml_command = "<action>SMS:infoSMSAccount</action>";
		$this->sendSCPData($this->serverURL, $xml_command);
		$this->toPlain();
		return $this->responseXML;
	}
	//接收SMS短信接口
	function readSMS(){
		$xml_command = "<action>SMS:readSMS</action>";
		$this->sendSCPData($this->serverURL, $xml_command);
		$this->toPlain();
		return $this->responseXML;
	}
	/*
	 * //可以废弃不用
	 */
	function updateConf($username, $pass, $server){
		if(!file_exists(ROOT_PATH . 'api/sms/config.inc.php')) return "文件config.inc.php 不存在！";
		if(!is_writable(ROOT_PATH . 'api/sms/config.inc.php')) return "文件config.inc.php 不可写！请检查文件属性！";
		$fd = fopen(ROOT_PATH . 'api/sms/config.inc.php',"w");
		if(!$fd) return "文件config.inc.php 打不开, 请检查文件属性！";
		$line = '<? '."\n".
			'/** '."\n".
			' * 这是配置文件 '.
			' * $'."vcpserver		SCP服务器地址，测试服务器为testxml.todaynic.com，正式服务器为sms.todaynic.com "."\n".
			' * $'."vcpserverport	SCP服务器，在测试环境和真实环境，使用的接口均为20002 "."\n".
			' * $'."vcpsuser		时代互联提供的真实短信用户或测试用户 "."\n".
			' * $'."vcppassword		 时代互联提供的真实短信用户密码或测试用户密码 "."\n".
			' * '."\n".
			' * www.now.cn,Inc. http://www.now.cn '."\n".
			'**/ '."\n".
			'$'.'vcpserver="'.$server.'"; '."\n".
			'$'.'vcpserverport="20002"; '."\n".
			'$'.'vcpuser="'.$username.'"; '."\n".
			'$'.'vcppassword="'.$pass.'"; '."\n".
			'?> '."\n";
		if(fwrite($fd, $line) === FALSE ) return "文件config.inc.php 写入失败！请检查文件属性！";
		fclose($fd);
		return "1";
	}
}